﻿// Double.cpp: 实现文件
//

#include "pch.h"
#include "RockSccissorsPapers.h"
#include "afxdialogex.h"
#include "Double.h"
#include "GUIDE.h"


// Double 对话框

IMPLEMENT_DYNAMIC(Double, CDialogEx)

Double::Double(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DOUBLE, pParent)
{

}

Double::~Double()
{
}

void Double::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(Double, CDialogEx)
	ON_BN_CLICKED(IDC_DBROCK, &Double::OnBnClickedDbrock)
	ON_BN_CLICKED(IDC_DBSCIS, &Double::OnBnClickedDbscis)
	ON_BN_CLICKED(IDC_DBPAPER, &Double::OnBnClickedDbpaper)
	ON_BN_CLICKED(IDC_DBCONF, &Double::OnBnClickedDbconf)
	ON_BN_CLICKED(IDC_DBRETRY, &Double::OnBnClickedDbretry)
	ON_BN_CLICKED(IDC_GUIDE, &Double::OnBnClickedGuide)
END_MESSAGE_MAP()

BOOL Double::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	AIChoice1 = 0;
	AIChoice2 = 0;
	AIChoiceFinal = 0;
	MyChoice1 = 0;
	MyChoice2 = 0;
	MyChoiceFinal = 0;
	Stage = 1;
	ScoreDB = 0;
	FScore();
	MyChoiceT = "你的选择：";
	AIChoiceT = "电脑的选择：";
	GetDlgItem(IDC_DBROCK)->EnableWindow(TRUE);
	GetDlgItem(IDC_DBSCIS)->EnableWindow(TRUE);
	GetDlgItem(IDC_DBPAPER)->EnableWindow(TRUE);
	GetDlgItem(IDC_DBCONF)->EnableWindow(FALSE);
	SetDlgItemText(IDC_DBEND, _T(""));
	return TRUE; 
}

// Double 消息处理程序
void Double::FScore()
{
	Scoreboard.Format(_T("目前得分: %d"), ScoreDB);
	SetDlgItemText(IDC_DBSCORE, Scoreboard);
}

void Double::OnBnClickedDbrock()
{
	if (Stage == 1 || Stage == 2)
	{
		GetDlgItem(IDC_DBROCK)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBSCIS)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBPAPER)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBCONF)->EnableWindow(TRUE);
		if (Stage == 1)
			MyChoice1 = 1;
		else
			MyChoice2 = 1;
		Choice = "   石头";
		MyChoiceT2.Format(_T("%s%s"), MyChoiceT, Choice);
		SetDlgItemText(IDC_DBMYCHOICE, MyChoiceT2);
	}
	else
	{
		MyChoiceFinal = 1;
		GetDlgItem(IDC_DBCONF)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBROCK)->EnableWindow(FALSE);
		if (MyChoice1 == 2 || MyChoice2 == 2)
			GetDlgItem(IDC_DBSCIS)->EnableWindow(TRUE);
		if (MyChoice1 == 3 || MyChoice2 == 3)
			GetDlgItem(IDC_DBPAPER)->EnableWindow(TRUE);
		SetDlgItemText(IDC_DBMYCHOICE, _T("你的选择：石头"));
	}
}

void Double::OnBnClickedDbscis()
{
	if (Stage == 1 || Stage == 2)
	{
		GetDlgItem(IDC_DBROCK)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBSCIS)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBPAPER)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBCONF)->EnableWindow(TRUE);
		if (Stage == 1)
			MyChoice1 = 2;
		else if (Stage == 2)
			MyChoice2 = 2;
		Choice = "   剪刀";
		MyChoiceT2.Format(_T("%s%s"), MyChoiceT, Choice);
		SetDlgItemText(IDC_DBMYCHOICE, MyChoiceT2);
	}
	else
	{
		MyChoiceFinal = 2;
		GetDlgItem(IDC_DBCONF)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBSCIS)->EnableWindow(FALSE);
		if (MyChoice1 == 1 || MyChoice2 == 1)
			GetDlgItem(IDC_DBROCK)->EnableWindow(TRUE);
		if (MyChoice1 == 3 || MyChoice2 == 3)
			GetDlgItem(IDC_DBPAPER)->EnableWindow(TRUE);
		SetDlgItemText(IDC_DBMYCHOICE, _T("你的选择：剪刀"));
	}
}

void Double::OnBnClickedDbpaper()
{
	if (Stage == 1 || Stage == 2)
	{
		GetDlgItem(IDC_DBROCK)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBSCIS)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBPAPER)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBCONF)->EnableWindow(TRUE);
		if (Stage == 1)
			MyChoice1 = 3;
		else if (Stage == 2)
			MyChoice2 = 3;
		Choice = "   布";
		MyChoiceT2.Format(_T("%s%s"), MyChoiceT, Choice);
		SetDlgItemText(IDC_DBMYCHOICE, MyChoiceT2);
	}
	else
	{
		MyChoiceFinal = 3;
		GetDlgItem(IDC_DBCONF)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBPAPER)->EnableWindow(FALSE);
		if (MyChoice1 == 1 || MyChoice2 == 1)
			GetDlgItem(IDC_DBROCK)->EnableWindow(TRUE);
		if (MyChoice1 == 2 || MyChoice2 == 2)
			GetDlgItem(IDC_DBSCIS)->EnableWindow(TRUE);
		SetDlgItemText(IDC_DBMYCHOICE, _T("你的选择：布"));
	}
}

int Double::Func(int a)
{
	if (a == 2 || a == 3)
		return(a - 1);
	else
		return 3;
}

void Double::OnBnClickedDbconf()
{
	if (Stage == 1)
	{
		GetDlgItem(IDC_DBROCK)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBSCIS)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBPAPER)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBCONF)->EnableWindow(FALSE);
		Sleep(750);
		MyChoiceT = MyChoiceT2;
		Stage++;
		srand((unsigned)time(NULL));
		AIChoice1 = 1 + rand() % 3;
		if (AIChoice1 == 1)
			Choice = "   石头";
		else if (AIChoice1 == 2)
			Choice = "   剪刀";
		else
			Choice = "   布";
		AIChoiceT2.Format(_T("%s%s"), AIChoiceT, Choice);
		AIChoiceT = AIChoiceT2;
		SetDlgItemText(IDC_DBAICHOICE, AIChoiceT);
		Sleep(750);
		GetDlgItem(IDC_DBROCK)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBSCIS)->EnableWindow(TRUE);
		GetDlgItem(IDC_DBPAPER)->EnableWindow(TRUE);
		SetDlgItemText(IDC_TURNS, _T("第二轮"));
	}
	else if (Stage == 2)
	{
		GetDlgItem(IDC_DBROCK)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBSCIS)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBPAPER)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBCONF)->EnableWindow(FALSE);
		Sleep(750);
		MyChoiceT = MyChoiceT2;
		Stage++;
		if (Func(MyChoice1) != AIChoice1)
			AIChoice2 = Func(MyChoice1);
		else
		{
			srand((unsigned)time(NULL));
			AIChoice2 = 1 + rand() % 3;
			if (AIChoice2 == AIChoice1)
				AIChoice2 = AIChoice2 % 3 + 1;
		}
		if (AIChoice2 == 1)
			Choice = "   石头";
		else if (AIChoice2 == 2)
			Choice = "   剪刀";
		else
			Choice = "   布";
		AIChoiceT2.Format(_T("%s%s"), AIChoiceT, Choice);
		AIChoiceT = AIChoiceT2;
		SetDlgItemText(IDC_DBAICHOICE, AIChoiceT);
		Sleep(750);
		SetDlgItemText(IDC_TURNS, _T("第三轮"));
		if (MyChoice1 == 1 || MyChoice2 == 1)
			GetDlgItem(IDC_DBROCK)->EnableWindow(TRUE);
		if (MyChoice1 == 2 || MyChoice2 == 2)
			GetDlgItem(IDC_DBSCIS)->EnableWindow(TRUE);
		if (MyChoice1 == 3 || MyChoice2 == 3)
			GetDlgItem(IDC_DBPAPER)->EnableWindow(TRUE);
	}
	else
	{
		GetDlgItem(IDC_DBROCK)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBSCIS)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBPAPER)->EnableWindow(FALSE);
		GetDlgItem(IDC_DBCONF)->EnableWindow(FALSE); 
		Sleep(750);
		srand((unsigned)time(NULL));
		AIChoiceFinal = rand() % 2;
		if (AIChoiceFinal == 1)
			AIChoiceFinal = AIChoice2;
		else
			AIChoiceFinal = AIChoice1;
		if (AIChoiceFinal == 1)
			SetDlgItemText(IDC_DBAICHOICE, _T("电脑的选择：石头"));
		else if (AIChoice1 == 2)
			SetDlgItemText(IDC_DBAICHOICE, _T("电脑的选择：剪刀"));
		else
			SetDlgItemText(IDC_DBAICHOICE, _T("电脑的选择：布"));
		Sleep(750);
		if (AIChoiceFinal==MyChoiceFinal)
			SetDlgItemText(IDC_DBEND, _T("平局！"));
		else if (Func(MyChoiceFinal) == AIChoiceFinal)
		{
			SetDlgItemText(IDC_DBEND, _T("你输了！"));
			ScoreDB--;
		}
		else
		{
			SetDlgItemText(IDC_DBEND, _T("你赢了！"));
			ScoreDB++;
		}
		FScore();
	}
}

void Double::OnBnClickedDbretry()
{
	// TODO: 在此添加控件通知处理程序代码
	AIChoice1 = 0;
	AIChoice2 = 0;
	AIChoiceFinal = 0;
	MyChoice1 = 0;
	MyChoice2 = 0;
	MyChoiceFinal = 0;
	Stage = 1;
	SetDlgItemText(IDC_DBEND, _T(""));
	MyChoiceT = "你的选择：";
	AIChoiceT = "电脑的选择：";
	SetDlgItemText(IDC_TURNS, _T("第一轮"));
	SetDlgItemText(IDC_DBMYCHOICE, _T("你的选择："));
	SetDlgItemText(IDC_DBAICHOICE, _T("电脑的选择："));
	GetDlgItem(IDC_DBROCK)->EnableWindow(TRUE);
	GetDlgItem(IDC_DBSCIS)->EnableWindow(TRUE);
	GetDlgItem(IDC_DBPAPER)->EnableWindow(TRUE);
	GetDlgItem(IDC_DBCONF)->EnableWindow(FALSE);
	SetDlgItemText(IDC_EXIT2, _T("将军走此小道 ↑"));
}

void Double::OnBnClickedGuide()
{
	// TODO: 在此添加控件通知处理程序代码
	Sleep(500);
	ShowWindow(SW_HIDE);
	Sleep(1000);
	GUIDE RSPDlg;
	RSPDlg.DoModal();
	ShowWindow(SW_SHOW);
}
